package com.lxw.manage_cms.controller;

import com.lxw.api.cms.CmsPageControllerApi;
import com.lxw.framework.domain.cms.CmsPage;
import com.lxw.framework.domain.cms.CmsSite;
import com.lxw.framework.domain.cms.request.QueryPageRequest;
import com.lxw.framework.domain.cms.response.CmsCode;
import com.lxw.framework.domain.cms.response.CmsPageResult;
import com.lxw.framework.exception.ExceptionCast;
import com.lxw.framework.model.response.CommonCode;
import com.lxw.framework.model.response.QueryResponseResult;
import com.lxw.framework.model.response.QueryResult;
import com.lxw.manage_cms.service.ICmsPageService;
import com.lxw.manage_cms.service.ICmsSiteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("page")
public class CmsPageController implements CmsPageControllerApi {

    @Autowired
    ICmsPageService cmsPageService;

    @Autowired
    ICmsSiteService cmsSiteService;

    @GetMapping("/list/{page}/{pageSize}")
    @Override
    public QueryResponseResult findList(@PathVariable Integer page
            , @PathVariable Integer pageSize, QueryPageRequest qpr) {

        //分页条件查询
        Page<CmsPage> list = cmsPageService.findList(page,pageSize,qpr);

        //封装类返回值
        QueryResult<CmsPage> pageQueryResult = new QueryResult<>();
        pageQueryResult.setList(list.getContent());
        pageQueryResult.setTotal(list.getTotalElements());
        QueryResponseResult responseResult = new QueryResponseResult(CommonCode.SUCCESS,pageQueryResult);


        return responseResult;
    }

    @PutMapping("/add")
    @Override
    public CmsPageResult add(@RequestBody CmsPage cmsPage) {

        //非空校验 ！！！
        //添加页面信息
        CmsPage cmsPage1 = cmsPageService.add(cmsPage);
        CmsPageResult result = new CmsPageResult(CommonCode.SUCCESS,cmsPage1);
        return result;
    }

    @GetMapping("/findById/{pageId}")
    @Override
    public CmsPageResult findById(@PathVariable String pageId) {

        //校验非空
        if (StringUtils.isEmpty(pageId)){
            throw new RuntimeException("页面ID为空");
        }

        CmsPage cmsPage = cmsPageService.findById(pageId);
        CmsPageResult cmsPageResult = new CmsPageResult(CommonCode.SUCCESS,cmsPage);
        return cmsPageResult;
    }


    @PostMapping("/save")
    @Override
    public CmsPageResult savePage(@RequestBody CmsPage cmsPage) {
        //更新页面信息 判断id不为空
        if (StringUtils.isEmpty(cmsPage.getSiteId())){
            throw new RuntimeException("页面ID为空");
        }
        //调用更新接口
        CmsPage cmsPage1 = cmsPageService.savePage(cmsPage);
        CmsPageResult result = new CmsPageResult(CommonCode.SUCCESS, cmsPage1);
        return result;
    }

    @DeleteMapping("/del/{pageId}")
    @Override
    public CmsPageResult delPage(@PathVariable String pageId) {
        //判断非空
        if (StringUtils.isEmpty(pageId)){
            ExceptionCast.cast(CmsCode.CMS_COURSE_PAGEIDNOTEXISTS);
        }

        try{
            cmsPageService.delPage(pageId);
            return new CmsPageResult(CommonCode.SUCCESS,null);
        }catch(Exception e){
            e.printStackTrace();
            return new CmsPageResult(CommonCode.FAIL,null);
        }


    }


    @Override
    @GetMapping("/postPage/{pageId}")
    public CmsPageResult postPage(@PathVariable String pageId) {

        //1.将html写入到gridFs 通知consumer
        cmsPageService.postPage(pageId);
        return new CmsPageResult(CommonCode.SUCCESS,null);
    }


}
